import axios from 'axios'

import { Message } from 'element-ui'


const instance = axios.create({
    baseURL: 'http://127.0.0.1:8127/',
    timeout: 5000
})

//请求拦截器
instance.interceptors.request.use(
    config =>{
        config.headers['Authorization'] = localStorage.getItem('token');
        return config;
    },error => {
        console.log(error);
        return Promise.reject(error)
    }
)


instance.interceptors.response.use(
    res => {
        const data = res.data
        if(data && data.code !== 200){
            alert(`错误code ${data.code}: ${data.message}`)
            return Promise.reject(new Error(data.message))
        }else{
            return data
        }
    }, error => {
        console.log(error);
        return Promise.reject(error)       
    }
)

export default instance;